package yyds.sniarbtej;

import yyds.sniarbtej.transformer.TransformerManager;
import yyds.sniarbtej.utils.JarUtil;

import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.URI;
import java.util.jar.JarFile;

public class Agent {

    private static boolean loaded = false;

    public static void premain(String args, Instrumentation instr) {
        loadAgent(args, instr, false);
    }

    public static void agentmain(String args, Instrumentation instr) {
        loadAgent(args, instr, true);
    }

    public static void printVersionInfo() {
        System.out.print("\n  ============================================================================  \n\n    sniarbtej YYDS 2024.2.5 R\n\n  ============================================================================  \n\n");
    }

    private static void loadAgent(String args, Instrumentation instr, boolean attachmode) {
        if (loaded) {
            System.out.println("loaded... return");
        } else {
            printVersionInfo();

            try {
                loaded = true;
                URI location = JarUtil.getJarLocation();
                File jar = new File(location.getPath());
                instr.appendToBootstrapClassLoaderSearch(new JarFile(jar));
                TransformerManager.createTransformers(new Environment(instr, jar, args, attachmode));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        if (args.length == 0) {
            AppUtils.showUsage();
        } else if (args[0].equals("-genkey")) {
            String licenseId = "SNIARBTEJ";
            String username = "JetBrains YYDS";
            String expire = "2048-10-24";
            String prodcuts = LicenseGenerator.getProducts();

            for (String arg : args) {
                if (arg.startsWith("-id=")) {
                    licenseId = arg.substring(4);
                } else if (arg.startsWith("-user=")) {
                    username = arg.substring(6);
                } else if (arg.startsWith("-exp=")) {
                    expire = arg.substring(5);
                } else if (arg.startsWith("-products=")) {
                    prodcuts = arg.substring(10);
                }
            }

            args = prodcuts.split(",");
            System.out.println("Your license key will be generated with the following details:");
            System.out.println("License ID: " + licenseId);
            System.out.println("Username: " + username);
            System.out.println("Expire Date: " + expire);
            System.out.println("Products: " + String.join(",", args));

            try {
                String key = LicenseGenerator.createLicenseKey(licenseId, username, expire, args);
                System.out.println("Your license key is:");
                System.out.println(key);
            } catch (Exception e) {
                System.out.println("Generate License Key Error: " + e.getLocalizedMessage());
            }

            System.out.println();
        } else if (args[0].equals("-list")) {
            AppUtils.showAvailableProducts();
        } else {
            AppUtils.showUsage();
        }
    }
}
